package gjtx8;

import java.io.File;

public class CalculatedSize {
    public static long getSize(File file){
        long size=0;
        if (file.isDirectory()){
            File[] list=file.listFiles();
            for (File s:list){
                size=size+getSize(s);
            }
        }else {
            size = size + file.length();
        }
        return size;
    }

    public static void main(String[] args) {
        File f=new File("D:\\新建文件夹");
        System.out.println("目录占用的空间大小："+CalculatedSize.getSize(f)*1.0/1024/1024+"MB");
    }
}
